﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;
using System.Security.Authentication;

namespace Legazpi.Payroll.Web.Helpers
{
    public static class UserHelper
    {
        /// <summary>
        /// Checks that the user is signed in and returns the user name
        /// </summary>
        /// <remarks>In Windows authentication, it removes the domain name</remarks>
        /// <returns></returns>
        public static string ValidateAndGetUserName()
        {
            IIdentity id = HttpContext.Current.User.Identity;

            if (!id.IsAuthenticated)
            {
                throw new AuthenticationException("User not Authenticated");
            }

            string[] splits = id.Name.Split('\\');
            return splits.Last();
        }
    }
}